home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7028 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: armltd.co.uk!dseal
  2. From: dseal@armltd.co.uk (David Seal)
  3. Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
  4. Subject: Re: Access carry flag from C
  5. Date: 20 Feb 1996 17:46:51 GMT
  6. Organization: Advanced RISC Machines Ltd
  7. Message-ID: <4gd1eb$nti@doc.armltd.co.uk>
  8. References: <Dn1C9z.DGv.0.net@indra.com>     <1996Feb1922.17.19.879@koobera.math.uic.edu> <31298D20.41C6@bazis.nl> <ARTHUR.96Feb20143404@gold.Smallworld.co.uk>
  9. NNTP-Posting-Host: sun11.armltd.co.uk
  10.  
  11. arthur@Smallworld.co.uk (Arthur Chance) writes:
  12.  
  13. >In article <31298D20.41C6@bazis.nl> Franz Korntner <fkorntne@bazis.nl> writes:
  14. >> j+k will overflow when the result exceeds MAXINT
  15. >> 
  16. >> Thus:  "if (j+k > MAXINT) overflow();" but the operation is undefined
  17. >> if the result overflows, so the expression needs rewriting to make sure
  18. >> this doesn't happen. The result is then "if (j>MAXINT-k) overflow();".
  19. >
  20. >As we must be talking about signed ints, because unsigned can't cause
  21. >undefined behaviour by overflow, if k < 0, then MAXINT-k overflows.
  22. >
  23. >Basically, the C *standard* is useless on things like signed overflow
  24.  
  25. Not entirely, but you have to be very careful to "guard" everything
  26. you do. E.g. in the above case, you can precede the "(j>MAXINT-k)"
  27. test with "(k>=0) &&" to ensure that you get a false result when k is
  28. negative.
  29.  
  30. I believe the following is a safe test for signed overflow of j+k in
  31. C (assuming <limits.h> has been included):
  32.  
  33.   ((k>=0) ? (j > INT_MAX-k) : (j < INT_MIN-k))
  34.  
  35. (with extra brackets around each "j" and each "k" if you use it as a
  36. macro expansion, of course). The idea is that:
  37.  
  38. * If k is non-negative, then j+k >= j as mathematical numbers. Since j
  39.   is in the signed range, this means that j+k can only overflow
  40.   positively. So we want to know whether j+k > INT_MAX, which can be
  41.   transformed into j > INT_MAX-k, and we know that INT_MAX-k won't
  42.   overflow if k is non-negative.
  43.  
  44. * If k is negative, then j+k < j as mathematical numbers. Since j
  45.   is in the signed range, this means that j+k can only overflow
  46.   negatively. So we want to know whether j+k < INT_MIN, which can be
  47.   transformed into j < INT_MIN-k, and we know that INT_MIN-k won't
  48.   overflow if k is negative.
  49.  
  50. Fortunately, as others have pointed out, the test for unsigned
  51. overflow (which is usually what the C flag is about) is much simpler,
  52. due to C defining the properties of unsigned arithmetic more fully.
  53. The usual test is to compare the result of the addition with either of
  54. the operands; you've got overflow if and only if the result is less
  55. than the operand.
  56.  
  57. David Seal
  58. dseal@armltd.co.uk
  59.